package com.gxc.base64;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.springframework.util.Base64Utils;

/**
 * @author 宫新程
 * @since 2019/1/29 9:27
 */
public class Base64Test {
  public static void main(String[] args) throws Exception {

    File pic = new File("C:\\Users\\Administrator\\Desktop\\2.bmp");
    String fileType = pic.getName().substring(pic.getName().lastIndexOf(".") + 1);
    System.out.println("File Name is ：" + fileType);

    /** 将图片文件转换成base64字符串. */
    FileInputStream fis = new FileInputStream(pic);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;
    byte[] bytes = new byte[1024];
    while ((len = fis.read(bytes)) != -1) {
      bos.write(bytes, 0, len);
    }
    byte[] fileBytes = bos.toByteArray();
    String fileBytesStr = Base64Utils.encodeToString(fileBytes);
    System.out.println(fileBytesStr);
    bos.close();
    fis.close();

    File dir = new File("C:\\Users\\Administrator\\Desktop\\h");
    if (!dir.exists()) {
      dir.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream(new File(dir, "hello3.".concat(fileType)));
    fos.write(fileBytes);
    fos.close();
  }

  //  public static void main(String[] args) {
  //    String baseCode =
  // "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZW1iZXJfaWQiOjIwODcwMSwicmV0YWlsX21lbWJlcl9pZCI6MCwicmV0YWlsX2N1c3RvbWVyX2lkIjowLCJ1c2VyX25hbWUiOiI4ODAwMTM4MTc2IiwibWVtYmVyX25hbWUiOiLphrTpmbXluILlpKfpmpzplYfmgJ3liKnnlLXlmajllYbooYwiLCJhdXRob3JpdGllcyI6WyJyb2xlIl0sImNsaWVudF9pZCI6InByb2RfY2xvdWRfYXBpX2dhdGV3YXkiLCJzaG9wX2lkIjpudWxsLCJhdWQiOlsieWxoLWNsb3VkLXNlcnZpY2UtZ29vZHMiLCJ5bGgtY2xvdWQtc2VydmljZS1iaSIsInlsaC1jbG91ZC1zZXJ2aWN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-Nv0ht0tJAe_-yw";
  //    String[] splitArray = baseCode.split("\\.");
  //
  //    for (String s : splitArray) {
  //      byte[] bytes = Base64Utils.decodeFromString(s);
  //      System.out.println(new String(bytes));
  //    }
  //  }
}
